package com.yenlien.traceyun.business.dao;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.yenlien.traceyun.business.entity.Box;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.Date;
import java.util.List;
import java.util.Map;

public interface BoxDao extends BaseJpaDao<Box> {

    public Box findBySourceCode(String code);

    @Query("select box.sourceCode from Box box where box.subpackageId=?1")
    public List<String> findBySubId(String subId);

    @Query("select box.sourceCode from Box box where box.subpackageBatch=?1")
    public List<String> findBySubBatch(String subBatch);

    List<Box> findByCreatetimeBetweenAndCompanyIdIn(Date createtime, Date createtime2, List<String> companyId);

    List<Box> findByCompanyId(String companyId);

    @Query(value = "select COUNT(source_code) source_code from t_plant_box where company_id=?1", nativeQuery = true)
    public int fuCode(String companyId);


    @Query(value = "select count(id) from t_plant_box where subpackage_batch=?1", nativeQuery = true)
    int isActivationSum2(String subpackageBatch);

    @Query(value = "select id,company_id,createtime,last_step,user_id,address,batch," +
            "company,platform,product_name,sc_time,source_code,activation,app_id,subpackage_batch,print_msg_id,subpackage_id " +
            "from t_plant_box where source_code=?1", nativeQuery = true)
    Box findSourceCode(String code);

    //激活符码
    @Modifying
    @Query(value = "update t_plant_box set activation=true,subpackage_id=?3,subpackage_batch=?1 where source_code=?2", nativeQuery = true)
    void updateBoxCode(String subpackageBatch, String sourceCode, String subpackageId);

    @Query(value = "select distinct id,company_id from t_plant_box where subpackage_batch=?1", nativeQuery = true)
    Map<String, Object> findByBatch(String subpackageBatch);


    @Transactional
    @Modifying
    @Query(value = "update t_plant_box set source_code=?1,createtime=?2 where subpackage_batch=?3", nativeQuery = true)
    void updateBoxdownload(String sourceCode, Date createtime, String subpackageBatch);

    @Transactional
    @Modifying
    @Query(value = "delete from t_plant_box where subpackage_batch=?1", nativeQuery = true)
    void deleteBatch(String subpackageBatch);

    @Query(value = "select id,subpackage_batch,company_id,createtime,source_code,print_msg_id,subpackage_id,activation,app_id from t_plant_box where subpackage_batch=?1", nativeQuery = true)
    List<Box> findBySubpackageBatch(String batch);
}
